package com.lulu.baselib.util;

import android.text.TextUtils;

/**
 * Author: XuChang
 * Date: 2019/10/20 20:23
 * Email: 123286473@qq.com
 * Description:
 * History:
 */
public class StringUtil {
    public static boolean isNotEmpty(String str) {
        return !isEmpty(str);
    }

    public static boolean isEmpty(String str) {
        return TextUtils.isEmpty(str);
    }

    /**
     * 把phone格式从13888888888,转换为 138 8888 8888
     *
     * @param phone
     * @return
     */
    public static String formatPhone(String phone) {
        if (phone.length() != 11) return phone;
        StringBuffer sb = new StringBuffer();
        sb.append(phone.substring(0, 3));
        sb.append(" ");
        sb.append(phone.substring(3, 7));
        sb.append(" ");
        sb.append(phone.substring(7, 11));
        return sb.toString();
    }

    /**
     * 获取没有尾数部分，包括没有小数点后的0
     *
     * @param number
     * @return
     */
    public static String getNoMantissa(String number) {
        if (StringUtil.isEmpty(number))
            return "";
        int lastIndex = number.length();
        if (number.contains(".")) {
            for (int i = number.length() - 1; i >= number.indexOf("."); i--) {
                if ('0' == (number.charAt(i)) || '.' == (number.charAt(i))) {
                    lastIndex = i;
                } else {
                    break;
                }
            }
        }
        return number.substring(0, lastIndex);
    }
}
